package com.testharness.module;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class SolidPetroStock {
	private String fileUrl;

	public SolidPetroStock(String fileUrl) {
		this.fileUrl = fileUrl;
	}

	public InputStream downloadFile() throws Exception {
		System.out.println("Downloading File for : "+ fileUrl);
		URL url = new URL(fileUrl);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("GET");
		System.out.println("Response Code : " + connection.getResponseCode());
		return connection.getInputStream();
	}

	public void process(String fileName) throws Exception {
		InputStream inputStream = downloadFile();
		Workbook workbook = WorkbookFactory.create(inputStream);
		Sheet sheet = workbook.getSheetAt(0);
		int rowNo = 0;
		boolean isFirstRecord = true;
		Long previousClosingStock = 0L;
		int noOfWrongRecords = 0;
		int totalRecordsChecked = 0;
		System.out.println("Sr.No. | OpeningStock | Purchase | Sales | Closing \n");
		for (Row row : sheet) {
			rowNo++;
			if (rowNo > 2) {
				long srNo = (long) row.getCell(0).getNumericCellValue();
				long openingStock = (long) row.getCell(3).getNumericCellValue();
				long purchase = (long) row.getCell(4).getNumericCellValue();
				long sale = (long) row.getCell(5).getNumericCellValue();
				long closing = (long) row.getCell(6).getNumericCellValue();

				if (isFirstRecord) {
					isFirstRecord = false;
					if ((openingStock - purchase) + sale != closing) {
						System.out.println(srNo + " | " + openingStock + " | " + purchase + " | " + sale + " | " + closing + "\n");
						noOfWrongRecords++;
					}

				} else {
					if (openingStock != previousClosingStock || (openingStock - purchase) + sale != closing) {
						System.out.println(srNo + " | " + openingStock + " | " + purchase + " | " + sale + " | " + closing + "\n");
						noOfWrongRecords++;
					}
				}
				totalRecordsChecked++;
				previousClosingStock = closing;
			}
		}
		System.out.println("--------------------------------------------------");
		System.out.println("No of Recoreds Tested : " + totalRecordsChecked);
		if (noOfWrongRecords > 0) {
			System.out.println("No of Invalid Recoreds : " + noOfWrongRecords);
		} else {
			System.out.println("Testing Complete. All records are valid.");
		}
		File file = new File(fileName);
		FileOutputStream fileOut = new FileOutputStream(fileName);
		workbook.write(fileOut);
		fileOut.flush();
		fileOut.close();
		System.out.println("Test file Saved at :" + file.getAbsolutePath());
	}

	public static void main(String... args) {
		String url = "http://localhost/excel-report/index.php/reportgenerator/generatespstock/";

		if (args.length > 0 && args[0].equals("remote")) {
			url = "http://demo-websiteapps.eu.pn/index.php/reportgenerator/generatespstock/";
		}
		String startDate = "01-01-2014";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
			String date = sdf.format(new Date());
			String endDate = date;

			SimpleDateFormat sdf1 = new SimpleDateFormat("HH-mm-ss");
			String time = sdf1.format(new Date());
			String fileName = "test_" + startDate + "To" + endDate + "_" + time + ".xlsx";
			SolidPetroStock test = new SolidPetroStock(url + startDate + "/" + endDate);
			test.process(fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
